import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { FlowworkauditDetailModelComponent } from '../flowworkauditDetailModel/flowworkauditDetailModel.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { TaskService } from '../task/Task.service';

import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { ProfileServiceProxy, OutsourcingWorkFlowServiceProxy, TaskServiceProxy, WorkflowServiceProxy } from '@shared/service-proxies/service-proxies';
import { from } from 'rxjs';

@Component({
  selector: 'processingOrderList',
  templateUrl: './ProcessingOrderList.component.html',
  styleUrls: ['./ProcessingOrderList.component.css']
})
export class ProcessingOrderListComponent extends AppComponentBase {
  @ViewChild('flowworkauditDetailModel') flowworkauditDetailModel: FlowworkauditDetailModelComponent;
  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('createOrEditModal') modal: ModalDirective;
  active = false;
  saving = false;
  filterText = '';

  constructor(injector: Injector,
    private _outSourcingworkflowServiceProxy: OutsourcingWorkFlowServiceProxy,
    private _taskServiceProxy: TaskServiceProxy,
    private _workflowServiceProxy: WorkflowServiceProxy
  ) {
    super(injector);
  }

  show(code: string): void {
    console.log(code);
    this.filterText = code;
    this.active = true;
    this.modal.show();

    this.getTasks();
  }
  close(): void {
    this.active = false;
    this.modal.hide();
  }
  onShown(): void {

  }
  getTasks(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }
    this.primengTableHelper.showLoadingIndicator();
    this._outSourcingworkflowServiceProxy.getProcessingListByOutsourcingCode(
      this.filterText,
      //this.primengTableHelper.getSorting(this.dataTable),
      'OrderCode',
      // this.primengTableHelper.getMaxResultCount(this.paginator, event),
      10,
      //this.primengTableHelper.getSkipCount(this.paginator, event),
      0
    )
      .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator()))
      .subscribe(result => {
        console.log(result);
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();

        _.forEach(result.items, viewItem => {
          console.log(viewItem.taskForModelId);
          if (!!viewItem.taskForModelId) {
            this._taskServiceProxy.getTaskForEdit(viewItem.taskForModelId)
              .subscribe(userResult => {
                console.log(userResult);
                viewItem.taskCode = userResult.displayName;
                viewItem.taskName = userResult.code;
                viewItem.picNo = userResult.picNo;
              });
          } else {
            viewItem.taskCode = '-';
            viewItem.taskName = '-';
            viewItem.picNo = '-';
          }
          if (viewItem.workflowId !== '') {
            this._workflowServiceProxy.getWorkflowByWorkflowid(viewItem.workflowId)
              .subscribe(WorkflowResult => {
                console.log(WorkflowResult);
                viewItem.applyType = WorkflowResult;
              });
          } else {
            viewItem.applyType = '-';
          }
        });
      });
  }

  // Detail(records) {
  //   this.flowworkauditDetailModel.show(records.applyType, records.workflowId);
  // }


}
